package projectVersion;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
	public static final String APP_PATH = "ns-parent\\ns-datum\\src\\main\\resources\\config\\env\\app.properties";
	public static final String EDITORMD_PATH = "ns-parent\\ns-static\\src\\main\\webapp\\res\\libs\\editor.md\\js\\editormd.min.js";
	public static final String DATASOURCE_PATH = "ns-parent\\ns-system\\src\\main\\resources\\config\\env\\dataSource.properties";

	public static void main(String[] args) throws IOException {
		new ProjectVersionHandler(APP_PATH).handle();
		
		if (args != null && args.length == 1) {
			String arg = args[0];
			Integer choose = Integer.parseInt(arg);
			
			// dataSource
			configChoose(choose, DATASOURCE_PATH, DATASOURCE_FILENAME_MAP);
			
			// 0/dev, 1/prod, 2/company
			// app.properties文件
			configChoose(choose, APP_PATH, APPCONFIG_FILENAME_MAP);
			
			// editormd
			configChoose(choose, EDITORMD_PATH, EDITORMD_FILENAME_MAP);
		}
	}
	
	public static final Map<Integer, String> APPCONFIG_FILENAME_MAP = new LinkedHashMap<Integer, String>() {
		private static final long serialVersionUID = 3393313088095293481L;
		{
			put(0, "resource/app-dev.properties");
			put(1, "resource/app-prod.properties");
			put(2, "resource/app-comp.properties");
			put(3, "resource/app-mblog.properties");
		}
	};
	
	public static final Map<Integer, String> EDITORMD_FILENAME_MAP = new LinkedHashMap<Integer, String>(){
		private static final long serialVersionUID = 6267330760206455195L;
		{
			put(0, "resource/editormd.min-dev&prod.js");
			put(1, "resource/editormd.min-dev&prod.js");
			put(2, "resource/editormd.min-comp.js");
			put(3, "resource/editormd.min-mblog.js");
		}
	};
	
	public static final Map<Integer, String> DATASOURCE_FILENAME_MAP = new LinkedHashMap<Integer, String>(){
		private static final long serialVersionUID = 6267330760206455195L;
		{
			put(0, "resource/dataSource-dev.properties");
			put(1, "resource/dataSource-prod.properties");
			put(2, "resource/dataSource-comp.properties");
			put(3, "resource/dataSource-mblog.properties");
		}
	};
	
	public static void configChoose(int choose, String path, Map<Integer, String> map) {
		File f = new File(path);
		InputStream in = null;
		String fileName = map.get(choose);
		
		if (fileName != null) {
			in = ClassLoader.getSystemResourceAsStream(fileName);
			FileUtils.fileCopy(in, f);
		}
	}
}
